package com.beemans.topon.kts

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.LifecycleOwner

/**
 * @author ti
 * @date 2022/11/23.
 */
/**
 * 获取LifecycleOwner
 */
inline val LifecycleOwner.lifecycleOwnerEx: LifecycleOwner
    get() = when (this) {
        is Fragment -> {
            try {
                this.viewLifecycleOwner
            } catch (e: IllegalStateException) {
                this
            }
        }
        else -> this
    }

/**
 * 获取上下文
 */
val LifecycleOwner.context: FragmentActivity?
    get() {
        return when (this) {
            is Fragment -> activity
            is FragmentActivity -> this
            else -> null
        }
    }